//
//  VCOfDoTest.m
//  LCOnePsychology
//
//  Created by li cheng on 16/9/2.
//  Copyright © 2016年 li cheng. All rights reserved.
//

#import "VCOfDoTest.h"
#import "BaseTableView.h"
#import "TVCellOfDoTest.h"
#import "MJMacro.h"
#import "TVCellOfBelowInformationDoTest.h"
#import "ZWYNetTool.h"
#import "DoTestBelowInformationData.h"
#import "DoTestBelowInformationRootClass.h"
#import "UIImageView+WebCache.h"
#import "VCOfBeginDoTest.h"
#import "MJRefresh.h"
@interface VCOfDoTest ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) DoTestBelowInformationRootClass *doTestBelowInformationRootClass;
@property (nonatomic, strong) BaseTableView *doTestTableView;
@property (nonatomic, strong) NSMutableArray<DoTestBelowInformationData *> *mArrayOfData;
@end

@implementation VCOfDoTest

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    // 数据处理
    [self handleData];
    // 设置返回按钮
    [self handleBackBUtton];
    // 创建 tableVeiw
    [self createTableView];
}
#pragma mark - 数据处理
- (void)handleData{
//    _mArrayOfData = [NSMutableArray array];
    [ZWYNetTool GET:@"http://yiapi.xinli001.com/yi/test-list.json?limit=20&offset=0&key=e110509c8317557ef50ce777d107f4b3&v=1&_platform=android&sign=2b8e446f6e7ca39c46e3f70ae87d0fc4&_version=4.0&token=0d4eeb8732f9ae2c73401002570ceb93" andBody:@"limit=20&offset=0&key=e110509c8317557ef50ce777d107f4b3&v=1&_platform=android&sign=2b8e446f6e7ca39c46e3f70ae87d0fc4&_version=4.0&token=0d4eeb8732f9ae2c73401002570ceb93" andHeader:nil andResponse:ZWYJSON andSuccessBlock:^(id result) {
        _doTestBelowInformationRootClass = [DoTestBelowInformationRootClass modelDic:result];
        _mArrayOfData = _doTestBelowInformationRootClass.data.mutableCopy;
        [self.doTestTableView reloadData];
    } andFailureBlock:^(NSError *error) {
        
    }];
}
#pragma mark - 设置返回按钮
- (void)handleBackBUtton{
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"< 返回" style:UIBarButtonItemStylePlain target:self action:@selector(back:)];
}
#pragma mark - 返回的实现
- (void)back:(UIBarButtonItem *)barButton{
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}
#pragma mark - 创建tableView
- (void)createTableView{
    self.doTestTableView = [[BaseTableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64) style:UITableViewStylePlain];
    [self.view addSubview:_doTestTableView];
    self.doTestTableView.delegate = self;
    self.doTestTableView.dataSource = self;
    [self.doTestTableView registerClass:[TVCellOfDoTest class] forCellReuseIdentifier:@"TVCellOfDoTest"];
    [self.doTestTableView registerNib:[UINib nibWithNibName:@"TVCellOfBelowInformationDoTest" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"TVCellOfBelowInformationDoTest"];
    // 下拉刷新
    self.doTestTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
    // 上拉加载
    self.doTestTableView.mj_footer = [MJRefreshAutoFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
}
#pragma mark - 下拉刷新
- (void)loadNewData{
    [self handleData];
    [self.doTestTableView.mj_header endRefreshing];
}
#pragma mark - 上拉加载
- (void)loadMoreData{
    static int i = 5;
    [ZWYNetTool GET:[NSString stringWithFormat:@"http://yiapi.xinli001.com/yi/test-list.json?limit=20&offset=%d&key=e110509c8317557ef50ce777d107f4b3&v=1&_platform=android&sign=2b8e446f6e7ca39c46e3f70ae87d0fc4&_version=4.0&token=0d4eeb8732f9ae2c73401002570ceb93", i] andBody:@"limit=20&offset=0&key=e110509c8317557ef50ce777d107f4b3&v=1&_platform=android&sign=2b8e446f6e7ca39c46e3f70ae87d0fc4&_version=4.0&token=0d4eeb8732f9ae2c73401002570ceb93" andHeader:nil andResponse:ZWYJSON andSuccessBlock:^(id result) {
        _doTestBelowInformationRootClass = [DoTestBelowInformationRootClass modelDic:result];
        _mArrayOfData = _doTestBelowInformationRootClass.data.mutableCopy;
        [self.doTestTableView reloadData];
    } andFailureBlock:^(NSError *error) {
        
    }];
    i = i + 5;
    [self.doTestTableView.mj_footer endRefreshing];
}
#pragma mark - tableView Sections 返回分区的个数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}
#pragma mark - tableView 代理方法 row 数量的确定
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return 1;
    } else {
        return _mArrayOfData.count;
    }
}
#pragma mark - tableView 代理方法 cell 的赋值
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        TVCellOfDoTest *cell = [tableView dequeueReusableCellWithIdentifier:@"TVCellOfDoTest"];
        return cell;
    } else {
        TVCellOfBelowInformationDoTest *cellOfBelowInformationDoTest = [tableView dequeueReusableCellWithIdentifier:@"TVCellOfBelowInformationDoTest"];
        [cellOfBelowInformationDoTest.coverImageView sd_setImageWithURL:[NSURL URLWithString:_mArrayOfData[indexPath.row].cover] placeholderImage:[UIImage imageNamed:@"0"]];
        cellOfBelowInformationDoTest.titleLabel.text = _mArrayOfData[indexPath.row].title;
        cellOfBelowInformationDoTest.viewnumLabel.text = _mArrayOfData[indexPath.row].viewnum;
        cellOfBelowInformationDoTest.commentnumLabel.text = _mArrayOfData[indexPath.row].commentnum;
        return cellOfBelowInformationDoTest;
    }
}
#pragma mark - tableView 代理方法 row 的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return SCREEN_HEIGHT / 7;
}
#pragma mark - tableView 代理方法 点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 1) {
        VCOfBeginDoTest *vcOfBeginDoTest = [[VCOfBeginDoTest alloc] init];
        vcOfBeginDoTest.coverString = _doTestBelowInformationRootClass.data[indexPath.row].cover;
        vcOfBeginDoTest.titleString = _doTestBelowInformationRootClass.data[indexPath.row].title;
        vcOfBeginDoTest.contentString = _doTestBelowInformationRootClass.data[indexPath.row].content;
        vcOfBeginDoTest.viewnumString = _doTestBelowInformationRootClass.data[indexPath.row].viewnum;
        vcOfBeginDoTest.myID = _doTestBelowInformationRootClass.data[indexPath.row].ID;
        [self presentViewController:vcOfBeginDoTest animated:YES completion:^{
            
        }];
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
